package com.hyx.api.service.impl.pc_admin;

import java.util.List;
import java.util.Arrays;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.hyx.api.domain.RefundOrder;
import com.hyx.api.domain.bo.RefundOrderBo;
import com.hyx.api.domain.vo.RefundOrderVo;
import com.hyx.api.mapper.RefundOrderMapper;
import com.hyx.api.service.pc_admin.IRefundOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;

/**
 * 退单记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-21
 */
@RequiredArgsConstructor
@Service
public class RefundOrderServiceImpl implements IRefundOrderService
{
    private final RefundOrderMapper refundOrderMapper;

    /**
     * 查询退单记录
     *
     * @param id 退单记录主键
     * @return 退单记录
     */
    @Override
    public RefundOrderVo selectRefundOrderById(Long id)
    {
        //TODO 表以外的字段，写入vo
        return refundOrderMapper.selectVoById(id);
    }

    /**
     * 查询退单记录列表
     *
     * @param bo 退单记录
     * @return 退单记录
     */
    @Override
    public List<RefundOrderVo> selectRefundOrderList(RefundOrderBo bo)
    {
        LambdaQueryWrapper<RefundOrder> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtils.isNotEmpty(bo.getRefundNo()), RefundOrder::getRefundNo, bo.getRefundNo());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getRecycleOrderId()), RefundOrder::getRecycleOrderId, bo.getRecycleOrderId());
        lqw.eq(ObjectUtils.isNotEmpty(bo.getStatus()), RefundOrder::getStatus, bo.getStatus());
        return refundOrderMapper.selectVoList(lqw);
    }

    /**
     * 新增退单记录
     *
     * @param bo 退单记录
     * @return 结果
     */
    @Override
    public int insertRefundOrder(RefundOrderBo bo)
    {
        RefundOrder refundOrder = tranEntityBeforeSave(bo);
        validEntityBeforeSave(refundOrder);
        return refundOrderMapper.insert(refundOrder);
    }

    /**
     * 修改退单记录
     *
     * @param bo 退单记录
     * @return 结果
     */
    @Override
    public int updateRefundOrder(RefundOrderBo bo)
    {
        RefundOrder refundOrder = tranEntityBeforeSave(bo);
        return refundOrderMapper.updateById(refundOrder);
    }

    /**
     * 批量删除退单记录
     *
     * @param ids 需要删除的退单记录主键
     * @return 结果
     */
    @Override
    public int deleteRefundOrderByIds(Long[] ids)
    {
        return refundOrderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RefundOrder entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     *   保存前的数据转换
     */
    private RefundOrder tranEntityBeforeSave(RefundOrderBo bo) {
        //TODO 做一些数据转换
        RefundOrder refundOrder = new RefundOrder();
        BeanUtils.copyProperties(bo, refundOrder);
        return refundOrder;
    }
}

